﻿package com.toncen.samepms.system.dao;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.toncen.samepms.basic.action.UserAction;
import com.toncen.samepms.basic.domain.Organization;
import com.toncen.samepms.basic.domain.User;
import com.toncen.samepms.common.AbstractDao;
import com.toncen.samepms.system.domain.FunctionPermission;
import com.toncen.samepms.system.domain.Module;
import com.toncen.samepms.system.domain.Role;

/**
 * 描述：功能权限表MONKEY_FUNCTION_PERMISSION的dao文件
 * 
 * @author tch
 */
public class FunctionPermissionDao extends AbstractDao<FunctionPermission> {

	public FunctionPermissionDao() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.comit.common.db.BaseDao#initialize()
	 */
	@Override
	protected void initialize() {
		setStatementPrefix("FunctionPermission");
		setStatementDivision(".");
	}

	/**
	 * 获取用户全部模块权限id集合
	 * 
	 * @param user
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Set<Long> getModuleFunctionPermissions(User user) throws Exception {
		Set<Long> permissionSet = new HashSet<Long>();
		Map<String, String> parameters = new HashMap<String, String>();
		// FunctionPermission functionPermission = null;
		if (user != null) {// 获取用户模块权限
			// functionPermission = new FunctionPermission();
			// functionPermission.setSubjectType(User.class.getName());
			// functionPermission.setSubjectId(user.getId());
			// functionPermission.setResourceType(Module.class.getName());
			// permissionSet.addAll(getResourceFunctionPermissions(functionPermission));
			parameters.put("UserSubjectType", User.class.getName());
			parameters.put("UserSubjectId", String.valueOf(user.getId()));
			parameters.put("UserResourceType", Module.class.getName());
		}
		if (user != null && user.getOrganization() != null) {// 获取组织模块权限
			// functionPermission.setSubjectType(Organization.class.getName());
			// functionPermission.setSubjectId(user.getOrganization().getId());
			// functionPermission.setResourceType(Module.class.getName());
			// permissionSet.addAll(getResourceFunctionPermissions(functionPermission));
			parameters.put("OrganizationSubjectType", Organization.class.getName());
			parameters.put("OrganizationSubjectId", String.valueOf(user.getOrganization().getId()));
			parameters.put("OrganizationResourceType", Module.class.getName());
		}
		if (user != null && user.getRoles() != null && user.getRoles().size() != 0) {// 获取角色模块权限
			StringBuffer RoleSubjectId = new StringBuffer();
			for (Role role : user.getRoles()) {
				// functionPermission = new FunctionPermission();
				// functionPermission.setSubjectType(Role.class.getName());
				// functionPermission.setSubjectId(role.getId());
				// functionPermission.setResourceType(Module.class.getName());
				// permissionSet.addAll(getResourceFunctionPermissions(functionPermission));
				RoleSubjectId.append(",");
				RoleSubjectId.append(role.getId());
			}
			parameters.put("RoleSubjectType", Role.class.getName());
			parameters.put("RoleSubjectId", RoleSubjectId.toString().replaceFirst(",", ""));
			parameters.put("RoleResourceType", Module.class.getName());
		}
		List<Long> results = (List) queryForList("getByUser", parameters);
		permissionSet.addAll(results);
		return permissionSet;
	}

	/**
	 * 获取资源id集合
	 * 
	 * @param functionPermission
	 * @return
	 * @throws Exception
	 */
	public Set<Long> getResourceFunctionPermissions(FunctionPermission functionPermission) throws Exception {
		List<FunctionPermission> permissionList = getByObject(functionPermission);
		Set<Long> permissionSet = new HashSet<Long>();
		for (FunctionPermission permission : permissionList) {
			permissionSet.add(permission.getResourceId());
		}
		return permissionSet;

	}

	/**
	 * 获取主体id集合
	 * 
	 * @param functionPermission
	 * @return
	 * @throws Exception
	 */
	public Set<Long> getSubjectFunctionPermissions(FunctionPermission functionPermission) throws Exception {
		List<FunctionPermission> permissionList = getByObject(functionPermission);
		Set<Long> permissionSet = new HashSet<Long>();
		for (FunctionPermission permission : permissionList) {
			permissionSet.add(permission.getSubjectId());
		}
		return permissionSet;

	}

	/**
	 * 根据对象查询
	 * 
	 * @param functionPermission
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<FunctionPermission> getByObject(FunctionPermission functionPermission) throws Exception {
		return findList("getByObject", functionPermission);
	}

	/**
	 * 根据主体保存功能权限:针对某一主体授予一批资源
	 * 
	 * @param subjectType
	 * @param subjectId
	 * @param resourceType
	 * @param resourtIds
	 * @throws Exception
	 */
	public void saveFunctionPermissionsBySubject(String subjectType, long subjectId, String resourceType, String[] resourceIds) throws Exception {
		FunctionPermission functionPermission = new FunctionPermission();
		functionPermission.setSubjectType(subjectType);
		functionPermission.setSubjectId(subjectId);
		functionPermission.setResourceType(resourceType);
		List<FunctionPermission> functionPermissions = getByObject(functionPermission);
		delete(functionPermissions);// 删除原来的权限
		if (resourceIds == null) {
			return;
		}
		for (String resourtId : resourceIds) {// 保存最新的
			if (resourtId != null && resourtId.trim().length() != 0) {
				functionPermission = new FunctionPermission();
				functionPermission.setSubjectType(subjectType);
				functionPermission.setSubjectId(subjectId);
				functionPermission.setResourceType(resourceType);
				functionPermission.setResourceId(Long.parseLong(resourtId));
				save(functionPermission);
			}
		}
		UserAction.clearCachePermissions();// 清除缓存
	}

	/**
	 * 根据资源保存功能权限：某一资源授权给一批主体
	 * 
	 * @param resourceType
	 * @param resourceId
	 * @param subjectType
	 * @param subjectIds
	 * @throws Exception
	 */
	public void saveFunctionPermissionsByResource(String resourceType, long resourceId, String subjectType, String[] subjectIds) throws Exception {
		FunctionPermission functionPermission = new FunctionPermission();
		functionPermission.setSubjectType(subjectType);
		functionPermission.setResourceId(resourceId);
		functionPermission.setResourceType(resourceType);
		List<FunctionPermission> functionPermissions = getByObject(functionPermission);
		delete(functionPermissions);// 删除原来的权限
		if (subjectIds == null) {
			return;
		}
		for (String subjectId : subjectIds) {// 保存最新的
			if (subjectId != null && subjectId.trim().length() != 0) {
				functionPermission = new FunctionPermission();
				functionPermission.setSubjectType(subjectType);
				functionPermission.setSubjectId(Long.parseLong(subjectId));
				functionPermission.setResourceType(resourceType);
				functionPermission.setResourceId(resourceId);
				save(functionPermission);
			}
		}
		UserAction.clearCachePermissions();// 清除缓存
	}
}
